world <-map_data("world") %>%filter(region !="Antarctica")
Cleaning and processing of data, outputing top 5 rows
data <-read.csv("/private/test.csv")head(data)
X Country Fertility Population Test CountryCode
1 1 Niger 6.82 24785.587 NER NER
2 2 Somalia 6.312 16801.170 SOM SOM
3 3 Chad 6.255 16910.218 TCD TCD
4 4 Democratic Republic of the Congo 6.156 94374.379 COD COD
5 5 Central African Republic 5.978 5414.014 CAF CAF
6 6 Mali 5.956 21561.299 MLI MLI
data <- data %>%mutate(Fertility =as.numeric(as.character(Fertility)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Fertility = as.numeric(as.character(Fertility))`.
Caused by warning:
! NAs introduced by coercion
head(data)
X Country Fertility Population Test CountryCode
1 1 Niger 6.820 24785.587 NER NER
2 2 Somalia 6.312 16801.170 SOM SOM
3 3 Chad 6.255 16910.218 TCD TCD
4 4 Democratic Republic of the Congo 6.156 94374.379 COD COD
5 5 Central African Republic 5.978 5414.014 CAF CAF
6 6 Mali 5.956 21561.299 MLI MLI
Baseline code for the Map
# Set figure dimensions#| fig.width=7#| fig.height=5# Display all Brewer palettesdisplay.brewer.all()
# Filter Gapminder data and join with world map data mapa_animado_3 <- data %>%right_join(world, by =c(Country ="region")) %>%ggplot(aes(long, lat, group = group, fill =`Fertility`)) +geom_polygon(color ="white", size =0.01) +theme_void() +scale_fill_distiller(palette ="Spectral", name ="Fertility Rate") +# Use a continuous palettelabs(title ="Population & Fertility Rate", subtitle ="year: 2022") +theme(plot.title =element_text(size =12, hjust =0.5),plot.subtitle =element_text(size =10, hjust =0.5),plot.caption =element_text(size =8, hjust =1) ) +coord_fixed(ratio =1.3)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Convert ggplot object to a ggplotly objectfig_3 <-ggplotly(mapa_animado_3)fig_3
Trying to add hovertext with data and data Frame
# Assuming 'world' and 'data' are already loaded and prepared# Create a hover text column in 'data'data$hover_text <-paste("Country: ", data$Country, "<br>Fertility Rate: ", data$Fertility, "<br>Population: ", data$Population)joined_data <- data %>%right_join(world, by =c(Country ="region"))# Proceed with your plottingmapa_animado_3 <- joined_data %>%ggplot(aes(x = long, y = lat, group = group, fill = Fertility, text = hover_text)) +# Added 'text' aesthetic for custom hover infogeom_polygon(color ="white", size =0.01) +theme_void() +scale_fill_distiller(palette ="Spectral", name ="Fertility Rate") +# Use a continuous palettelabs(title ="Population & Fertility Rate", subtitle ="year: 2022") +theme(plot.title =element_text(size =12, hjust =0.5),plot.subtitle =element_text(size =10, hjust =0.5),plot.caption =element_text(size =8, hjust =1) ) +coord_fixed(ratio =1.3)# Convert ggplot object to a ggplotly object with hoverinfofig_3 <-ggplotly(mapa_animado_3, tooltip ="text") # Specify 'text' to use the 'text' aesthetic for the tooltipfig_3
displaying my joined_data into a dataframe and remove repeated country names